package com.itheima.day11.otherio.test2;

import java.io.*;
import java.util.ArrayList;

public class StudentDemo2 {
    public static void main(String[] args) {
        Student s1 = new Student("张三", 23);
        Student s2 = new Student("李四", 24);
        Student s3 = new Student("王五", 25);

        ArrayList<Student> al = new ArrayList<>();
        al.add(s1);
        al.add(s2);
        al.add(s3);

        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("backBurner\\Student.txt"));
            oos.writeObject(al);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        ObjectInputStream ois = null;
        ArrayList<Student> al2 = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("backBurner\\Student.txt"));
            al2 = (ArrayList<Student>) ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        for (Student stu : al2) {
            System.out.println(stu);
        }

    }
}

